﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Nojin.Deployment
{
    public partial class ShowUpdateProgressForm :  Form
    {

        #region Fields
        private UpdateInfo updateInfo;
        private bool cancelApproved = false;
        #endregion

        #region Constructors
        public ShowUpdateProgressForm()
        {
            InitializeComponent();
        }
        public ShowUpdateProgressForm(UpdateInfo updateInfo) :this()
        {
            this.updateInfo = updateInfo;
        }
        #endregion

        #region Properties
        public UpdateInfo CurrentUpdateInfo
        {
            get { return updateInfo; }
            set
            {
                updateInfo = value;                 
            }
        }
        #endregion

        #region Methods

        public bool UpdateInfo(string filename, int currentFileNumber, int totalFiles, long bytesReceived, long totalBytesToReceive)
        {
            labelFileName.Text = filename;
            progressBarAllFiles.Maximum = totalFiles;
            progressBarAllFiles.Minimum = 1;
            progressBarAllFiles.Value = currentFileNumber;
            progressBarFile.Maximum = (int)totalBytesToReceive;
            progressBarFile.Minimum = 0;
            progressBarFile.Value = (int)bytesReceived;

            if (cancelApproved) // return value is whether to cancel or not
            {
                this.Close();
                return true;
            }
            else
                return false;
        }

        public void DownloadCompleted()
        {
            cancelApproved = true;
            Close();
        }

        private void buttonGoBackground_Click(object sender, EventArgs e)
        {
            GoBackGroundMode();
        }

        private void GoBackGroundMode()
        {
            showWindowToolStripMenuItem.Visible = true;            
            this.Visible = false;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void Cancel()
        {
            string message = Texts.DoYouWantToCancelDownloading;
            if (!updateInfo.Arbitary)
                message = Texts.DownloadingNewVersionIsArbitary + message;
            DialogResult dialogResult = MessageBox.Show(Texts.CancelingDownloadOfNewVersion,message,MessageBoxButtons.YesNo,MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                cancelApproved = true;
                this.Hide();
            }
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            if (!cancelApproved)
            {
                e.Cancel = true;
                GoBackGroundMode();
            }
            else
            {
                notifyIcon.Visible = false;
            }
        }

        private void cancelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void showWindowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //userClosed = true;
            this.Visible = true;
            showWindowToolStripMenuItem.Visible = false;
        }

        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = !this.Visible;
            showWindowToolStripMenuItem.Visible = !this.Visible;
        }
        
        #endregion
    }
}